﻿using System;
using System.Windows;
using System.Windows.Shapes;
using System.Windows.Input;
using System.Windows.Media;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace HexingColors
{
    public partial class MainPage : PhoneApplicationPage
    {
        int squareNumber;
        int winnerColor;
        string gameLevel;
        SolidColorBrush winnerFill;

        public MainPage()
        {
            InitializeComponent();

            appBarEasy = this.ApplicationBar.Buttons[0] as ApplicationBarIconButton;
            appBarMedium = this.ApplicationBar.Buttons[1] as ApplicationBarIconButton;
            appBarHard = this.ApplicationBar.Buttons[2] as ApplicationBarIconButton;
            appBarHelp = this.ApplicationBar.Buttons[3] as ApplicationBarIconButton;

            InitializeGame("Medium");    // Set to medium difficulty (4 squares) by default. Valid options: Easy, Medium, Hard
        }

        private void InitializeGame(string newLevel)
        {
            // Set game difficulty:
            setDifficultyLevel(newLevel);

            // Clear game board:
            PageTitle.Foreground = new SolidColorBrush(Color.FromArgb(255,255,255,255));
            txtStatus.Text = string.Empty;
            btnNewGame.Visibility = Visibility.Collapsed;
            Rectangle e;

            // Set random RGB colors:
            Random randObj = new Random();

            for (int c = 1; c <= squareNumber; c++)
            {
                // Find the squares:
                e = (Rectangle)FindName("recPanel" + gameLevel + c.ToString());

                // Restore the squares:
                e.Opacity = 100;
                e.IsHitTestVisible = true;

                // Paint the squares:
                e.Fill = new SolidColorBrush(Color.FromArgb(255, (byte)randObj.Next(256), (byte)randObj.Next(256), (byte)randObj.Next(256)));
            }            

            // Pick the winner:
            winnerColor = randObj.Next(1, squareNumber+1);
            e = (Rectangle)FindName("recPanel" + gameLevel + winnerColor.ToString());
            winnerFill = (SolidColorBrush)e.Fill;
            PageTitle.Text = "#" + winnerFill.Color.ToString().Substring(3);
        }

        private void panelSelected(object sender, MouseButtonEventArgs args)
        {
            Rectangle thisRec = (Rectangle)sender;
            SolidColorBrush thisFill = (SolidColorBrush)thisRec.Fill;

            if (winnerFill.Color == thisFill.Color)
            {
                txtStatus.Text = "¡Correcto!";
                PageTitle.Foreground = winnerFill;
                for (int c = 1; c <= squareNumber; c++)
                {
                    Rectangle e = (Rectangle)FindName("recPanel" + gameLevel + c.ToString());
                    if (c != winnerColor)
                    {
                        e.Opacity = 0;
                        e.IsHitTestVisible = false;
                    }
                }
                btnNewGame.Visibility = Visibility.Visible;
            }
            else
            {
                txtStatus.Text = "No, este color es #" + thisFill.Color.ToString().Substring(3);
                thisRec.Opacity = 0;
                thisRec.IsHitTestVisible = false;
            }
        }


        private void btnNewGame_Click(object sender, RoutedEventArgs e)
        {
            // Start a new game!
            InitializeGame(gameLevel);
        }

        private void setDifficultyLevel(string newLevel)
        {
            switch (newLevel)
            {
                case "Easy":
                    gameLevel = "Easy";
                    squareNumber = 2;

                    easyLevel.Visibility = Visibility.Visible;
                    easyLevel.IsHitTestVisible = true;
                    mediumLevel.Visibility = Visibility.Collapsed;
                    hardLevel.Visibility = Visibility.Collapsed;
                    break;

                case "Medium":
                    gameLevel = "Medium";
                    squareNumber = 4;

                    mediumLevel.Visibility = Visibility.Visible;
                    mediumLevel.IsHitTestVisible = true;
                    easyLevel.Visibility = Visibility.Collapsed;
                    hardLevel.Visibility = Visibility.Collapsed;
                    break;

                case "Hard":
                    gameLevel = "Hard";
                    squareNumber = 9;

                    hardLevel.Visibility = Visibility.Visible;
                    hardLevel.IsHitTestVisible = true;
                    easyLevel.Visibility = Visibility.Collapsed;
                    mediumLevel.Visibility = Visibility.Collapsed;
                    break;

                default:
                    gameLevel = "Medium";
                    squareNumber = 4;

                    mediumLevel.Visibility = Visibility.Visible;
                    mediumLevel.IsHitTestVisible = true;
                    easyLevel.Visibility = Visibility.Collapsed;
                    hardLevel.Visibility = Visibility.Collapsed;
                    break;
            }
        }

        private void newEasyGame(object sender, EventArgs e)
        {
            InitializeGame("Easy");
        }

        private void newMediumGame(object sender, EventArgs e)
        {
            InitializeGame("Medium");
        }

        private void newHardGame(object sender, EventArgs e)
        {
            InitializeGame("Hard");
        }

        private void showHelp(object sender, EventArgs e)
        {
            MessageBoxResult info = MessageBox.Show("Creado por @mauricioangulo\nInspirado en el juego 'What the Hex?' de Andy Yang.\n\nMás información en http://bit.ly/hMa27k", "Hexing Colors", MessageBoxButton.OK);
        }
    }
}
